package hhAcademicBowl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Scanner;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class GameData {
	
	private HHAC manager;
	
	String title;
	String[] normalCategories = new String[5];
	Question[][] normalQuestions = new Question[5][5];
	
	String[] doubleCategories = new String[5];
	Question[][] doubleQuestions = new Question[5][5];
	
	FinalJeopardy finaljeo;
	
	public GameData(HHAC man){
		manager = man;
		finaljeo = new FinalJeopardy(manager);
	}
	

	@Override
	public String toString() {
		return "GameData [title=" + title + ", normalCategories="
				+ Arrays.toString(normalCategories) + ", normalQuestions="
				+ Arrays.toString(normalQuestions) + ", doubleCategories="
				+ Arrays.toString(doubleCategories) + ", doubleQuestions="
				+ Arrays.toString(doubleQuestions) + "]";
	}


	public void addCategory(String categ) {
		for(int co = 0; co < 5; co++){
			if(normalCategories[co] == null){
				normalCategories[co] = categ;
				break;
			}
		}
	}
	
	public void loadFromFile(File file) {
		
		try {
			
			InputStream f = new FileInputStream(file);
			Scanner input = new Scanner(f, "UTF-8"); //TODO this is in case it's borked
			input.useDelimiter("\u001f");
			
			int que = 0;
			int cat = -1; //janky fix so it's 0-indexed see below
			
			
			/*
			 * NOTE: the unicode characters below are important
			 * \u001f is the entry delimiter (replacing : and ; in V1.0)
			 * \u001e is the data delimiter (replacing , in V1.0)
			 */
			while(input.hasNext()){
				String val = input.next().replaceAll("\n|\r\n", "");
				
				if(val.equals("!")){ //game (double or single jeopardy
					title = input.next().replaceAll("\n|\r\n", "");
					que = 0;
					cat = -1;
				}
				else if(val.equals("@")){ //category
					que = 0;
					cat++;
					normalCategories[cat] = input.next().replaceAll("\n|\r\n", "");
				}
				else if(val.equals("#")){
					String[] qData = input.next().replaceAll("\n|\r\n", "").split("\u001e");
					
					try { //This insures that the file is complete
						
						int value = Integer.parseInt(qData[2]); 
						boolean dd = Boolean.parseBoolean(qData[3]);
						normalQuestions[cat][que] = new Question(manager, qData[0], qData[1], value, dd);
					} catch (ArrayIndexOutOfBoundsException e) {
						
						JFrame fr = new JFrame();
						JOptionPane.showMessageDialog(fr,
							    "The file is incomplete. Please fill in all data in the Editor.",
							    "File Error",
							    JOptionPane.ERROR_MESSAGE);
						
						System.exit(0);
					}
					
					que++;
				}
				else if(val.equals("$")){
					que = 0;
					cat++;
					doubleCategories[cat] = input.next().replaceAll("\n|\r\n", "");
				}
				else if(val.equals("%")){
					String[] qData = input.next().replaceAll("\n|\r\n", "").split("\u001e");
					
					try {
						int value = Integer.parseInt(qData[2]);
						boolean dd = Boolean.parseBoolean(qData[3]);
						doubleQuestions[cat][que] = new Question(manager, qData[0], qData[1], value, dd);
					} catch (ArrayIndexOutOfBoundsException e) {
						JFrame fr = new JFrame();
						JOptionPane.showMessageDialog(fr,
							    "The file is incomplete. Please fill in all data in the Editor.",
							    "File Error",
							    JOptionPane.ERROR_MESSAGE);
						
						System.exit(0);
					}
					
					que++;
				}
				else if(val.equals("&")){
					//finaljeo.finalJeopardyQ = input.next().replaceAll("\n|\r\n", "");
					finaljeo.setFinalJeopardyQ(input.next().replaceAll("\n|\r\n", ""));
				}
				else if(val.equals("~")){
					//finaljeo.finalJeopardyA = input.next().replaceAll("\n|\r\n", "");
					finaljeo.setFinalJeopardyA(input.next().replaceAll("\n|\r\n", ""));
				}
				
				
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}


	public String getTitle() {
		return title;
	}


	public String[] getNormalCategories() {
		return normalCategories;
	}


	public Question[][] getNormalQuestions() {
		return normalQuestions;
	}


	public String[] getDoubleCategories() {
		return doubleCategories;
	}


	public Question[][] getDoubleQuestions() {
		return doubleQuestions;
	}


	public void setTitle(String title) {
		this.title = title;
	}


	public void setNormalCategories(String[] normalCategories) {
		this.normalCategories = normalCategories;
	}


	public void setNormalQuestions(Question[][] normalQuestions) {
		this.normalQuestions = normalQuestions;
	}


	public void setDoubleCategories(String[] doubleCategories) {
		this.doubleCategories = doubleCategories;
	}


	public void setDoubleQuestions(Question[][] doubleQuestions) {
		this.doubleQuestions = doubleQuestions;
	}
	
}
